/*
 * @lc app=leetcode.cn id=2 lang=golang
 *
 * [2] 两数相加
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
	ans := &ListNode{}
	p := ans
	flag := 0
	for l1 != nil || l2 != nil {
		var l1V, l2V int
		if l1 != nil {
			l1V = l1.Val
			l1 = l1.Next
		}
		if l2 != nil {
			l2V = l2.Val
			l2 = l2.Next
		}
		sum := flag + l1V + l2V

		temp := &ListNode{Val: sum % 10}
		flag = sum / 10

		p.Next = temp
		p = p.Next
	}
	if flag == 1 {
		p.Next = &ListNode{Val: 1}
	}
	return ans.Next
}

// @lc code=end

